//
//  LBExclusionFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 3/29/13.
//  Copyright (c) 2013 UIT. All rights reserved.
//

#include "LBExclusionColorFilter.h"
#include "LBGraphics.h"

char fShaderStrExclusionColor[] = ""
"precision mediump float;"

"varying vec2 vtexCoor;"
"varying vec4 vposition;"
"uniform sampler2D texture;"
"uniform vec4 color;"
"uniform float opacity;"


// exclusion function
"vec4 exclusion(vec4 base, vec4 blend, float opacity){"
"   vec4 result =  base;"

"   result.r = 0.5 - 2.0* (base.r - 0.5)*(blend.r - 0.5);"
"   result.g = 0.5 - 2.0* (base.g - 0.5)*(blend.g - 0.5);"
"   result.b = 0.5 - 2.0* (base.b - 0.5)*(blend.b - 0.5);"

"   result = mix(base, result, opacity);"

"   return result;"

"}"
// END exclusion function


"void main()"
"{"
"    lowp vec4 textureColor = texture2D(texture, vtexCoor);"
"    textureColor = exclusion(textureColor, color, opacity);"
"    gl_FragColor = textureColor;"
"}";

LBExclusionColorFilter::LBExclusionColorFilter(){
    init(0, fShaderStrExclusionColor);
    LBGraphics::getInstance()->useProgram(program);
    colorUniform = program->uniformIndex("color");
    opacityUniform = program->uniformIndex("opacity");
    setColor(1.0f, 1.0f, 1.0f);
    setOpactiy(1.0f);
}